package com.yw.pms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.yw.pms.dao.ProblemTypeDao;
import com.yw.pms.dao.common.Page;
import com.yw.pms.dao.entity.ProblemType;
import com.yw.pms.utils.CommonResp;

@Service
public class ProblemTypeService {

	private final static Logger logger = LoggerFactory.getLogger(ProblemTypeService.class);

	@Resource
	ProblemTypeDao problemTypeDao;

	public CommonResp<Boolean> addProblemType(ProblemType problemType) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			problemTypeDao.insert(problemType);
			commonResp.success();
			logger.info("操作员编号【{}】新增问题类型【{}】成功", problemType.getOperatorId(), problemType);
		} catch (Exception e) {
			logger.error("新增问题类型发生异常", e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<ProblemType>> queryProblemTypeList(ProblemType problemType) {
		problemType.setPage(new Page(problemType.getPageNo()));
		CommonResp<List<ProblemType>> commonResp = new CommonResp<List<ProblemType>>();
		try {
			List<ProblemType> listPage = problemTypeDao.listPage(problemType);
			commonResp.success();
			commonResp.setData(listPage);
			Map<String,Object> extraData = Maps.newHashMapWithExpectedSize(1);
			extraData.put("page", problemType.getPage());
			commonResp.setExtraData(extraData);
			logger.info("操作员ID【{}】查询问题类型信息【{}】成功",problemType.getOperatorId(),commonResp);
		} catch (Exception e) {
			logger.error("分页查询问题类型列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<ProblemType>> getAllProblemType(ProblemType problemType) {
		CommonResp<List<ProblemType>> commonResp = new CommonResp<List<ProblemType>>();
		try {
			List<ProblemType> listPage = problemTypeDao.listPage(problemType);
			commonResp.success();
			commonResp.setData(listPage);
			logger.info("查询问题类型信息【{}】成功",commonResp);
		} catch (Exception e) {
			logger.error("分页查询问题类型列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> deleteProblemType(String problemTypeId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Integer id = Integer.valueOf(problemTypeId);
			problemTypeDao.delete(id);
			commonResp.success();
			logger.info("删除问题类型【{}】成功",problemTypeId);
		} catch (Exception e) {
			logger.error("删除问题类型发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<ProblemType> get(String problemTypeId){
		CommonResp<ProblemType> commonResp = new CommonResp<ProblemType>();
		try {
			ProblemType problemType = problemTypeDao.getById(Integer.valueOf(problemTypeId));
			commonResp.success();
			commonResp.setData(problemType);
			logger.info("查询问题类型【{}】成功",problemTypeId);
		} catch (Exception e) {
			logger.error("查询问题类型信息系统异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> isProblemTypeNameUniq(String problemTypeName, String productId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			ProblemType problemType = new ProblemType();
			problemType.setProblemTypeName(problemTypeName);
			ProblemType problemType2 = problemTypeDao.get(problemType);
			if(problemType2 == null) {
				commonResp.success();
			} else {
				commonResp.fail();
			}
			logger.info("校验问题类型名称【{}】重复结果【{}】（成功为不存在，失败为存在）",problemTypeName,commonResp.getMsg());
			return commonResp;
		} catch (Exception e) {
			logger.error("校验问题类型名称发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> updateProblemType(ProblemType problemType) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			problemTypeDao.update(problemType);
			commonResp.success();
			logger.info("更新问题类型【{}】成功",problemType.getProblemTypeName());
		} catch (Exception e) {
			logger.error("更新问题类型发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
}
